#include "editwidget.h"
#include "widgets/ulineedit.h"
#include "widgets/upasswordlineedit.h"
#include "widgets/ufilechooseredit.h"
#include "widgets/usettingsgroup.h"
#include "widgets/usettingsitem.h"
#include "widgets/uswitchbutton.h"
#include "plugin/upluginmanager.h"
#include "plugin/uplugininfo.h"
#include "plugin/uiplugin.h"
#include "widgets/ucirclebutton.h"
#include "widgets/upagination.h"

#include <QHBoxLayout>

EditWidget::EditWidget(QWidget* parent) : UWidget(parent)
{
    setBackgroundRole(QPalette::Base);
    setRadius(8);

    ULineEdit* lineEdit = new ULineEdit(this);
    lineEdit->setMinimumHeight(35);
    UPassWordLineEdit* passWordLineEdit = new UPassWordLineEdit(this);
    passWordLineEdit->lineEidt()->setMinimumHeight(35);

    UFileChooserEdit* fileChooserEdit = new UFileChooserEdit(this);
    fileChooserEdit->lineEidt()->setMinimumHeight(35);

    connect(lineEdit, &ULineEdit::returnPressed, this, [ = ]()
    {
        if (lineEdit->text().isEmpty())
        {
            lineEdit->showAlertMessage("lineEdit isEmpty!!!");
        }
    });

    USettingsGroup* group = new USettingsGroup(this);
    for (int i = 0; i < 3; ++i)
    {
        auto item = new USettingsItem(group);
        item->setText(QString("USettingsItem%1").arg(i));
        item->setIcon(QIcon::fromTheme("title"));
        item->addWidget(new USwitchButton(item));
        group->appendItem(item);
    }

    QGridLayout* mainLayout = new QGridLayout;
    mainLayout->addWidget(lineEdit, 0, 0);
    mainLayout->addWidget(passWordLineEdit, 0, 1);
    mainLayout->addWidget(group, 1, 0);
    mainLayout->addWidget(fileChooserEdit, 1, 1);
    mainLayout->addWidget(new UCircleButton(QStyle::StandardPixmap::SP_DesktopIcon), 2, 0);
    setLayout(mainLayout);

    for (const auto &plugin : UPluginManager::instance()->allPlugins())
    {
        if (plugin->iPlugin())
        {
            QPushButton* button = qobject_cast<QPushButton*>(plugin->iPlugin()->createObject("test", this));
            button->setIcon(QIcon::fromTheme("title"));
            mainLayout->addWidget(button, 2, 1);
        }
    }

    UPagination* pageWidget = new UPagination(this);
    pageWidget->setButtonSize(QSize(30, 30));
    pageWidget->setMaxPage(10);
    mainLayout->addWidget(pageWidget, 3, 0);
}

EditWidget::~EditWidget()
{
}
